package com.sys.controller;

import com.sys.common.JsonResult;
import com.sys.common.P_Search;
import com.sys.common.U_Search;
import com.sys.dto.ProductDTO;
import com.sys.dto.UserDTO;
import com.sys.entity.ProductEntity;
import com.sys.entity.UserEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController extends BaseController{
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<UserEntity> login(String username, String password, HttpSession session){
        UserDTO userDTO = new UserDTO();


        UserEntity newuser = userDTO.findByName(username);
        String newpassword = newuser.getPassword();
        UserEntity user = new UserEntity();

        if(newpassword.equals(password)){
            session.setAttribute("uid", newuser.getId());
            session.setAttribute("username", newuser.getUsername());
            return new JsonResult<UserEntity>(OK,newuser);
        }else{
            return  null;
        }
    }
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<List<UserEntity>> findAll(){
        UserDTO userDTO = new UserDTO();
        List<UserEntity> all = userDTO.findAll();
        JsonResult<List<UserEntity>> listJsonResult = new JsonResult<>(EOK, all);
        System.out.println(listJsonResult);
        System.out.println(listJsonResult.getCode());
        System.out.println(listJsonResult.getMes());
        return new JsonResult<List<UserEntity>>(EOK, all);
    }
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Void> reg(UserEntity entity){
        UserDTO userDTO = new UserDTO();
        userDTO.save(entity);
        return new JsonResult<>(EOK);
    }
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Void> del(Integer id,HttpSession session){
        UserDTO userDTO = new UserDTO();
        userDTO.delete(id);
        return new JsonResult<>(EOK);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Void> update(UserEntity entity,HttpSession session){

        Integer uid = getUidFromSession(session);
        UserDTO userDTO = new UserDTO();
        userDTO.update(entity,uid);

        System.out.println("修改后的用户名是："+entity.getUsername());
        System.out.println("修改后的密码是："+entity.getPassword());

        return new JsonResult<>(EOK);
    }


    @RequestMapping(value = "/change-password", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<String> changePassword(String old_password, String new_password, HttpSession session){

        Integer uid = getUidFromSession(session);

        UserDTO userDTO =new UserDTO();
        UserEntity user = userDTO.findById(uid);
        if (user.getPassword().equals(old_password)){
            userDTO.changePassword(uid, new_password);
            return new JsonResult<>(EOK);
        }else {
            return new JsonResult<>(400,"密码错误");
        }
    }


    @RequestMapping(value = "/findsearch", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<List<UserEntity>> findsearch(U_Search u_search){
        System.out.println("搜索条件是："+u_search);
        String id = u_search.getId();
        String username = u_search.getUsername();
        String age = u_search.getAge();
        if (id != null && !id.equals("")){
            System.out.println("根据订单号查询");
            System.out.println("订单号是："+id);
            UserDTO userDTO = new UserDTO();
            List<UserEntity> byProperty = userDTO.findByProperty("id", id);
            return new JsonResult<List<UserEntity>>(EOK, byProperty);
        } else if (username != null && !username.equals("")) {
            System.out.println("根据商品名查询");
            System.out.println("商品名是："+username);
            UserDTO userDTO = new UserDTO();
            List<UserEntity> byProperty = userDTO.findByProperty("username", username);
            return new JsonResult<List<UserEntity>>(EOK, byProperty);
        } else if (age != null && !age.equals("")) {
            System.out.println("根据客户名查询");
            System.out.println("客户名是："+age);
            UserDTO userDTO = new UserDTO();
            List<UserEntity> byProperty = userDTO.findByProperty("age", age);
            return new JsonResult<List<UserEntity>>(EOK, byProperty);
        }else
            return new JsonResult<List<UserEntity>>(EOK);
    }
}

